package gao.xiaolei.controller;

import gao.xiaolei.entity.ResponseEnvelope;
import gao.xiaolei.service.TestStatusService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/common/testStatus")
public class TestStatusController {

    @Resource(name = "TestStatusService")
    TestStatusService testStatusService;

    @GetMapping("/describe/{code}")
    public ResponseEntity<ResponseEnvelope<String>> getDescribe(@PathVariable(name = "code", required = true) int code) {
        return new ResponseEntity<ResponseEnvelope<String>>(
                new ResponseEnvelope<String>(200, "success", testStatusService.getDescribeOfCode(code)),
                HttpStatus.OK);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(200));
    }
}
